package com.greenline.push.home.controllers;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.ReferenceConfig;
import com.greenline.push.services.PushCenterCommonService;
import com.greenline.push.services.dto.ResponseDTO;

@Controller
@RequestMapping("/test")
public class TestToolController {
    private static final String split = "@@@";

    @RequestMapping("")
    public String index() {
        return "test/run";
    }

    @RequestMapping("/createbroadcast")
    public void createbroadcast(HttpServletResponse response, String url, final String templateCode, final String domainCode,
            final String classCode, String param) {
        final String[] define = new String[1];
        final Map<String, String> paramMap = new HashMap<String, String>();
        String[] paramString = param.split(split);
        for (int i = 0; i < paramString.length; i++) {
            String parama = paramString[i];
            String entry[] = parama.split("=");
            if (entry.length == 2)
                paramMap.put(entry[0], entry[1]);
        }
        runPushService(url, new runMethod() {
            @Override
            public void run(PushCenterCommonService service) {
                ResponseDTO<Boolean> s = service.createBroadCast(classCode, templateCode, domainCode, paramMap, null);
                define[0] = String.format(
                        "content:<font color='red'>%s</font>,<br/>msg:<font color='red'>%s</font>,<br/>errMsg:<font color='red'>%s</font>",
                        s.getDataResult(), s.getMessage(), s.getErrorMsg());
            }

            @Override
            public String getContent() {
                return define[0];
            }

        }, response);
    }

    @RequestMapping("/senddomain")
    public void senddomain(HttpServletResponse response, String url, final String templateCode, final String domainCode, String param) {
        final String[] define = new String[1];
        final Map<String, String> paramMap = new HashMap<String, String>();
        String[] paramString = param.split(split);
        for (int i = 0; i < paramString.length; i++) {
            String parama = paramString[i];
            String entry[] = parama.split("=");
            if (entry.length == 2)
                paramMap.put(entry[0], entry[1]);
        }
        runPushService(url, new runMethod() {
            @Override
            public void run(PushCenterCommonService service) {
                ResponseDTO<Boolean> s = service.sendDomain(templateCode, domainCode, paramMap, null);
                define[0] = String.format(
                        "content:<font color='red'>%s</font>,<br/>msg:<font color='red'>%s</font>,<br/>errMsg:<font color='red'>%s</font>",
                        s.getDataResult(), s.getMessage(), s.getErrorMsg());
            }

            @Override
            public String getContent() {
                return define[0];
            }

        }, response);
    }

    @RequestMapping("/sendgroup")
    public void sendgroup(HttpServletResponse response, String url, final Long gid, final String templateCode, final String domainCode,
            String param) {
        final String[] define = new String[1];
        final Map<String, String> paramMap = new HashMap<String, String>();
        String[] paramString = param.split(split);
        for (int i = 0; i < paramString.length; i++) {
            String parama = paramString[i];
            String entry[] = parama.split("=");
            if (entry.length == 2)
                paramMap.put(entry[0], entry[1]);
        }
        runPushService(url, new runMethod() {
            @Override
            public void run(PushCenterCommonService service) {
                ResponseDTO<Boolean> s = service.sendGroup(gid, templateCode, domainCode, paramMap, null);
                define[0] = String.format(
                        "content:<font color='red'>%s</font>,<br/>msg:<font color='red'>%s</font>,<br/>errMsg:<font color='red'>%s</font>",
                        s.getDataResult(), s.getMessage(), s.getErrorMsg());
            }

            @Override
            public String getContent() {
                return define[0];
            }

        }, response);
    }

    @RequestMapping("/sends2s")
    public void sends2s(HttpServletResponse response, String url, final Long receiverUserId, final String templateCode,
            final String domainCode, String param) {
        final String[] define = new String[1];
        final Map<String, String> paramMap = new HashMap<String, String>();
        String[] paramString = param.split(split);
        for (int i = 0; i < paramString.length; i++) {
            String parama = paramString[i];
            String entry[] = parama.split("=");
            if (entry.length == 2)
                paramMap.put(entry[0], entry[1]);
        }
        runPushService(url, new runMethod() {
            @Override
            public void run(PushCenterCommonService service) {
                ResponseDTO<Boolean> s = service.send(receiverUserId, templateCode, domainCode, paramMap, null);
                define[0] = String.format(
                        "content:<font color='red'>%s</font>,<br/>msg:<font color='red'>%s</font>,<br/>errMsg:<font color='red'>%s</font>",
                        s.getDataResult(), s.getMessage(), s.getErrorMsg());
            }

            @Override
            public String getContent() {
                return define[0];
            }

        }, response);
    }

    @RequestMapping("/getdefine")
    public void getbizDefine(HttpServletRequest request, HttpServletResponse response, ModelMap map, String url, final String code)
            throws IOException {
        final String[] define = new String[1];
        runPushService(url, new runMethod() {
            @Override
            public void run(PushCenterCommonService service) {
                ResponseDTO<String> s = service.getBizDefine(code);
                define[0] = String.format(
                        "content:<font color='red'>%s</font>,<br/>msg:<font color='red'>%s</font>,<br/>errMsg:<font color='red'>%s</font>",
                        s.getDataResult(), s.getMessage(), s.getErrorMsg());
            }

            @Override
            public String getContent() {
                return define[0];
            }
        }, response);
    }

    // abstract
    private void runPushService(String url, runMethod call, HttpServletResponse response) {
        String s = null;
        ReferenceConfig<PushCenterCommonService> config = new ReferenceConfig<PushCenterCommonService>();
        config.setUrl(url + "com.greenline.push.services.PushCenterCommonService");
        config.setInterface(PushCenterCommonService.class);
        try {
            PushCenterCommonService service = config.get();
            call.run(service);
            config.destroy();
            s = call.getContent();
        } catch (Exception e1) {
            e1.printStackTrace();
            s = String.format("获取该环境服务失败.msg:<font color='red'>%s</font>", e1.getMessage());
        }
        response.setContentType("text/html;charset=UTF-8");
        try {
            response.getWriter().println(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // call back
    interface runMethod {
        void run(PushCenterCommonService service);

        String getContent();
    }
}
